#include "frozen-bubble.h"

SDL_Surface *screen = NULL;
SDL_Joystick *joysticks[10] = { NULL };
int num_joysticks;

FILE *logfile = NULL;

FBLevelSet *levelset = NULL;

void init ()
{
	int joys;
	
	#ifdef SDLJOY
	SDL_Joystick *stick;
	const char *stickname;
	#endif
	
	#ifdef PLATFORM_WII
	if (!fatInitDefault ())
	{
		fprintf (stderr, "Unable to initialize FAT\n");
		exit (EXIT_FAILURE);
	}
	
	if (!(logfile = fopen ("/frozen-bubble.log", "wb")))
	#endif
	#if defined(PLATFORM_LINUX) || defined(PLATFORM_WIN32)
	if (!(logfile = stdout))
	#endif
	{
		fprintf (stderr, "Unable to open log file\n");
		exit (EXIT_FAILURE);
	}
	
	fprintf (logfile, "init: Begin\n");
	
	if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO
	#ifdef SDLJOY
		| SDL_INIT_JOYSTICK
	#endif
		) < 0)
	{
		fprintf (logfile, "init: Unable to init SDL: %s\n", SDL_GetError ());
		exit (EXIT_FAILURE);
	}
	
	SDL_WM_SetCaption ("Frozen-Bubble 2", NULL);
	
	#ifdef PLATFORM_WII
	WPAD_Init ();
	#endif
	
	atexit (SDL_Quit);
	SDL_ShowCursor (SDL_DISABLE);
	
	screen = SDL_SetVideoMode (640, 480, 16, SDL_DOUBLEBUF);
	if (!screen)
	{
		fprintf (logfile, "init: Unable to set video: %s\n", SDL_GetError ());
		exit (EXIT_FAILURE);
	}
	
	SDL_FillRect (screen, 0, SDL_MapRGB (screen->format, 0, 0, 0));
	put_image (IMG_LOADING, 10, 10);
	SDL_Flip(screen);
	
	#ifdef SDLJOY
	joys = SDL_NumJoysticks ();
	fprintf (logfile, "init: Found %d joystick(s)\n", joys);
	
	for (int i = 0; i < joys; i++)
	{
		stick = SDL_JoystickOpen (i);
		stickname = SDL_JoystickName (i);
		
		if (stick)
		{
			fprintf (logfile, "\t%d: %s\n", i + 1, stickname ? stickname : "Unknown Joystick");
			
			joysticks[num_joysticks++] = stick;
		}
		else
			fprintf (logfile, "\t%d: %s [Unable to Open]\n", i + 1, stickname ? stickname : "Unknown Joystick");
	}
	#else
	joys = 0;
	#endif
	
	fprintf (logfile, "init: Loading levelset...\n");
	
	if (!(levelset = level_set_new ("levels")))
	{
		fprintf (logfile, "init: Failed to load levelset!\n");
		exit (EXIT_FAILURE);
	}
	
	fprintf (logfile, "init: Preloading images...\n");
	
	PRELOAD (IMG_BACK_START);
	PRELOAD_LIST (IMGTMPLT_ANIM_1PGAME, 1, 30);
	PRELOAD_LIST (IMGTMPLT_ANIM_P1P2, 1, 30);
	PRELOAD_LIST (IMGTMPLT_ANIM_NETGAME, 1, 89);
	
	fprintf (logfile, "init: Pre-rendering shooter images...\n");
	game_init_shooter ();
	
	fprintf (logfile, "init: Done\n");
}

void cleanup ()
{
	SDL_Quit ();
	
	#ifdef PLATFORM_WII
	fclose (logfile);
	#endif
	
	for (int i = 0; i < num_joysticks; i++)
	{
		SDL_JoystickClose (joysticks[i]);
		joysticks[i] = NULL;
	}
	
	exit (EXIT_SUCCESS);
}

#ifdef PLATFORM_WII
int SDL_main (int argc, char *argv[])
#else
int main (int argc, char *argv[])
#endif
{
	bool done = FALSE;	
	bool moveleft = FALSE, moveright = FALSE, movecenter = FALSE;
	init ();
	menu_start ();
	
	while (!done)
	{
		#ifdef PLATFORM_WII
		WPAD_ScanPads();
		u32 down = WPAD_ButtonsDown (0);
		u32 held = WPAD_ButtonsHeld (0);
		
		moveleft = game_running && (held & WPAD_BUTTON_UP);
		moveright = game_running && (held & WPAD_BUTTON_DOWN);
		movecenter = game_running && (held & WPAD_BUTTON_LEFT);
		
		if (down & WPAD_BUTTON_HOME)
		{
			done = TRUE;
		}
		else if (down & WPAD_BUTTON_RIGHT) // Up
		{
			widget_activate_prev (rootlayer);
		}
		else if (down & WPAD_BUTTON_LEFT) // Down
		{
			widget_activate_next (rootlayer);
		}
		else if (down & WPAD_BUTTON_1)
		{
			widget_layer_return (rootlayer);
		}
		else if (down & WPAD_BUTTON_2)
		{
			widget_press_active (rootlayer);
		}
		#endif
		
		#ifdef SDLKBD
		SDL_Event event;
		
		while (SDL_PollEvent (&event))
		{
			switch (event.type)
			{
			case SDL_QUIT:
				done = TRUE;
				break;
				
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym)
				{
				case SDLK_UP:
					widget_activate_prev (rootlayer);
					break;
					
				case SDLK_DOWN:
					if (game_running)
						movecenter = TRUE;
					else
						widget_activate_next (rootlayer);
					
					break;
					
				case SDLK_LEFT:
					moveleft = TRUE;
					break;
					
				case SDLK_RIGHT:
					moveright = TRUE;
					break;
					
				case SDLK_RETURN:
					widget_press_active (rootlayer);
					break;
					
				case SDLK_ESCAPE:
					widget_layer_return (rootlayer);
					break;
					
				default:
					break;
				}
				
				break;
				
			case SDL_KEYUP:
				switch (event.key.keysym.sym)
				{
				case SDLK_DOWN:
					movecenter = FALSE;
					break;
					
				case SDLK_LEFT:
					moveleft = FALSE;
					break;
					
				case SDLK_RIGHT:
					moveright = FALSE;
					break;
					
				default:
					break;
				}
				
			default:
				break;
			}
		}
		#endif
		
		if (moveleft)
			game_move_left ();
		else if (moveright)
			game_move_right ();
		else if (movecenter)
			game_move_center ();
		
		widget_layer_render (rootlayer);
		SDL_Flip(screen);
	}
	
	cleanup ();
	return 0;
}

